﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using static CommunicationClass.Model.SendReceiveEnum;

namespace CommunicationServer
{
    public partial class ServerSystem : Form
    {
        CommunicationClass.Server.ServerCommunication TCP = new CommunicationClass.Server.ServerCommunication();
        public ServerSystem()
        {
            InitializeComponent();

            TCP.JournalMessage += TCP_JournalMessage;
            TCP.ServerLogin += TCP_ServerLogin;
            TCP.LoadUserData += TCP_LoadUserData;
            TCP.ServerReceiveMessage += TCP_ServerReceiveMessage;
        }

        private bool TCP_ServerReceiveMessage(CommunicationClass.Model.MessageModel messageModel, SendOrReceiveTypeEnum MessageType, out string Message)
        {
            //发送消息：
            Message = "";

            this.BeginInvoke(new Action(() => {
                if (button3.Text == "关闭日志")
                {
                    textBox1.Text +="发送消息：发送人："+ messageModel .MsgSendUserID+ "接收人：" + messageModel.MsgReceiveUserID + "消息类型："+ MessageType .ToString()+ "\r\n";
                    textBox1.Select(this.textBox1.TextLength, 0);//光标定位到文本最后
                    textBox1.ScrollToCaret();//滚动到光标处

                }

            }), null);

            return true;
        }

        private string TCP_LoadUserData(CommunicationClass.Model.MessageUserLogin messageUser)
        {
            
            this.BeginInvoke(new Action(() => {
                if (button3.Text == "关闭日志")
                {
                    textBox1.Text += "加载基本信息：加载人：" + messageUser.MsgLoginUserID + "\r\n";
                    textBox1.Select(this.textBox1.TextLength, 0);//光标定位到文本最后
                    textBox1.ScrollToCaret();//滚动到光标处

                }

            }), null);
            return "服务器返回的Json数据";

        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (button1.Text == "启动服务")
            {
                button1.Text = "关闭服务";
                string message = "";
                timer1.Enabled = true;
                TCP.Open(out message);
                textBox1.Text += message + "\r\n";
                textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                textBox1.ScrollToCaret();//滚动到光标处
            }
            else
            {

                button1.Text = "启动服务";
                string message = "";
                TCP.Close(out message);
                timer1.Enabled = false;
                if (!string.IsNullOrEmpty(message))
                {
                    textBox1.Text += message + "\r\n";
                    textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                    textBox1.ScrollToCaret();//滚动到光标处
                }
            }

        }

        private bool TCP_ServerLogin(string UserID, string UserPwd, CommunicationClass.Model.MsgLoginTypeEnum MsgLoginTypeEnum, out string UserName, out string Message)
        {

            UserName = "sdzhuang";
            Message = "";
            return true;
        }

        private void TCP_JournalMessage(CommunicationClass.Server.ServerCommunication.JournalMessageEnum journalMessageEnum, string Message)
        {

            this.BeginInvoke(new Action(() => {
                if (button3.Text == "关闭日志")
                {
                    textBox1.Text += Message + "\r\n";
                    textBox1.Select(this.textBox1.TextLength, 0);//光标定位到文本最后
                    textBox1.ScrollToCaret();//滚动到光标处

                }

            }), null);


        }


        private void button3_Click(object sender, EventArgs e)
        {
            if (button3.Text == "启动日志")
            {
                button3.Text = "关闭日志";
            }
            else
            {
                button3.Text = "启动日志";
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            label4.Text = TCP.UserInfoTheQueueCount.ToString();
            label2.Text = TCP.UserInfoCount.ToString();
            label6.Text = TCP.MessageTheQueueCount.ToString();
        }
    }
}
